package com.stylefeng.guns.modular.waapp.tranObject;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ObjectTranMap {
	public static <T> List<Map<String, Object>> convertListBean2ListMap(List<T> beanList, Class<T> T)
	        throws Exception
	    {
	        List<Map<String, Object>> mapList = new ArrayList<>();
	        for (int i = 0, n = beanList.size(); i < n; i++)
	        {
	            Object bean = beanList.get(i);
	            Map<String, Object> map = convertBean2Map(bean);
	            mapList.add(map);
	        }
	        return mapList;
	    }
	
	public static Map<String, Object> convertBean2Map(Object bean)
	        throws IntrospectionException, IllegalAccessException, InvocationTargetException
	    {
	        Class<? extends Object> type = bean.getClass();
	        Map<String, Object> returnMap = new HashMap<>();
	        BeanInfo beanInfo = Introspector.getBeanInfo(type);
	        
	        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
	        for (int i = 0; i < propertyDescriptors.length; i++)
	        {
	            PropertyDescriptor descriptor = propertyDescriptors[i];
	            String propertyName = descriptor.getName();
	            if (!"class".equals(propertyName))
	            {
	                Method readMethod = descriptor.getReadMethod();
	                Object result = readMethod.invoke(bean, new Object[0]);
	                if (result != null)
	                {
	                    returnMap.put(propertyName, result);
	                }
	                else
	                {
	                    returnMap.put(propertyName, null);
	                }
	            }
	        }
	        return returnMap;
	    }
}
